package com.fdd.op.sdk.internal.util;

/**
 * url工具类
 *
 * @author Fadada
 * @date 2021/05/08
 */
public abstract class FddUrlUtils {
    /**
     * 等到请求url
     *
     * @throws
     * @Author: zhouj1@fadada.com
     * @Date: 2022/6/7
     * @Param: [serverUrl, apiMethodName, session, appParams]
     * @Return: java.lang.String
     */
    public static String getServerUrl(String serverUrl, String apiMethodName, String session, FddHashMap appParams) {
        if (apiMethodName.indexOf(StringUtils.SLASH) > 0) {
            apiMethodName = StringUtils.SLASH + apiMethodName;
        }
        String host = serverUrl.lastIndexOf(StringUtils.SLASH) == 0 ? replaceLast(serverUrl, StringUtils.SLASH, StringUtils.EMPTY) : serverUrl;
        return host + apiMethodName;
    }

    /**
     * 替换字符串里最后出现的元素
     *
     * @throws
     * @Param: [text, strToReplace, replaceWithThis]
     * @Return: java.lang.String
     */
    private static String replaceLast(String text, String strToReplace,
                                      String replaceWithThis) {
        return text.replaceFirst("(?s)" + strToReplace + "(?!.*?" + strToReplace
                + ")", replaceWithThis);
    }
}
